//
//  BHCircleReviewTextView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/6.
//

import UIKit
protocol BHCircleReviewTextViewDelegate {
    func reviewTextViewPublishReview(text: String, reply: Bool,replyModel: BHCircleReviewModel?)
}

class BHCircleReviewTextView: UIView {
    lazy var shadowTapView: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()
    
    lazy var replyBottomView: BHCircleReviewPresentTextView = {
        let view = BHCircleReviewPresentTextView.loadFromXib()
        view.delegate = self
        return view
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    var delegate: BHCircleReviewTextViewDelegate?
    
    
    // 记录是评论还是回复某人，默认评论
    fileprivate var replyAct: Bool = false

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.isHidden = true
        self.addSubview(self.shadowTapView)
        self.addSubview(self.replyBottomView)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
        self.shadowTapView.addViewAction {[weak self] (_) in
            self?.close()
        }
        self.layoutInit()
    }
    
    func layoutInit() {
        self.shadowTapView.frame = self.bounds
        self.replyBottomView.frame = CGRect(x: 0, y: self.bh_height, width: UIScreen.width, height: BHCircleReviewPresentTextView.height)
    }
    
    
    var reviewer: BHCircleReviewModel? {
        didSet {
            if reviewer == nil {
                self.replyAct = false
                self.replyBottomView.place_holder_label.text = String.reviewPlaceHolder
            }else {
                self.replyAct = true
                self.replyBottomView.place_holder_label.text = "回复 "+reviewer!.reviewerName
            }
        }
    }
    
    
    func becomeActive(_ replayActive: Bool) {
        self.isHidden = false
        
        self.replyAct = replayActive
        self.replyBottomView.becomeActive()
    }
    
    func publishComplete() {
        self.replyBottomView.text_view.text = ""
        self.replyAct = false
        self.replyBottomView.place_holder_label.isHidden = false
        self.replyBottomView.place_holder_label.text = String.reviewPlaceHolder
        self.replyBottomView.resignActive()
    }
    
    private func close() {
        self.replyAct = false
        self.replyBottomView.place_holder_label.text = String.reviewPlaceHolder
        self.replyBottomView.resignActive()
    }
    
    
    @objc func keyboardShow(_ notification: NSNotification) {
        let keyboardHeight = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
        
        let duration = (notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        
        UIView.animate(withDuration: duration) {
            self.replyBottomView.frame = CGRect(x: 0, y: UIScreen.height-BHCircleReviewPresentTextView.height-keyboardHeight, width: UIScreen.width, height: BHCircleReviewPresentTextView.height)
        }
    }
    
    @objc func keyboardHide(_ notification: NSNotification) {
        self.isHidden = true
        
        let duration = (notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        UIView.animate(withDuration: duration) {
            self.replyBottomView.frame = CGRect(x: 0, y: self.bh_height, width: UIScreen.width, height: BHCircleReviewPresentTextView.height)

        }
    }
}


extension BHCircleReviewTextView: BHCircleReviewPresentTextViewDelegate {
    func textViewPublishReview(reviewText: String) {
        self.delegate?.reviewTextViewPublishReview(text: reviewText, reply: self.replyAct,replyModel:self.reviewer)
    }
}
